package com.eastcom.common.db;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import com.eastcom.canicula.stope.utils.DateUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;


public class DataSourceFactory {

	private static String driverClass;
	
	private static String url;
	
	private static String userName; 
	
	private static String passWord;
	
	private static int min_size;
	
	private static int max_size;
	
	private static int time_out;
	
	private static int max_statements;
	
	private static int acquire_increment;
	
	private static int maxConnectionAge;
	
	private static int max_idle_time;
	
	private static int idle_test_period;
	
	private static boolean validateOncheckIn;

	private static boolean autoReconnect;
	
	private static boolean autoCommitOnClose;

	private static boolean autoReconnectForPools;

	private static boolean is_connection_validation_required;
	  
	private static int batch_size;

	private static int fetch_size;
	
	private static int numHelperThreads;
	
	private static String peferredTestQuery;
	
	private static DataSourceFactory instance=null;
	
	private static List<ComboPooledDataSource> dataSources=new ArrayList<ComboPooledDataSource>();;
	
	private static String path;
	
    public static void setFilePath(String filePath){
    	path=filePath;
    }
	
	public static void initial() throws Exception{
		try {
			File file=new File(path==null?"config/jdbc.properties":path);
			FileInputStream inStream=new FileInputStream(file);
			Properties props=new Properties();
			props.load(inStream);
			
			driverClass=props.getProperty("driver_class");
			url=props.getProperty("url");
			userName=props.getProperty("username");
			passWord=props.getProperty("password");
			
			min_size=Integer.parseInt(props.getProperty("min_size"));
			max_size=Integer.parseInt(props.getProperty("max_size"));
			time_out=Integer.parseInt(props.getProperty("time_out"));
			max_statements=Integer.parseInt(props.getProperty("max_statements"));
			acquire_increment=Integer.parseInt(props.getProperty("acquire_increment"));
			
			peferredTestQuery=props.getProperty("peferredTestQuery");
			maxConnectionAge=Integer.parseInt(props.getProperty("maxConnectionAge"));
			max_idle_time=Integer.parseInt(props.getProperty("max_idle_time"));
			idle_test_period=Integer.parseInt(props.getProperty("idle_test_period"));
			validateOncheckIn=Boolean.parseBoolean(props.getProperty("validateOncheckIn"));
			autoCommitOnClose=Boolean.parseBoolean(props.getProperty("autoCommitOnClose"));
			numHelperThreads=Integer.parseInt(props.getProperty("numHelperThreads"));
		} 
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static DataSourceFactory getInstance(){
		if(instance==null)
			instance=new DataSourceFactory();
		
		return instance;
	}
	
	public DataSource createDataSource(){
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		
		try {
			dataSource.setDriverClass(driverClass);
			dataSource.setJdbcUrl(url);
			dataSource.setUser(userName);
			dataSource.setPassword(passWord);
			
			dataSource.setMinPoolSize(min_size);
			dataSource.setMaxPoolSize(max_size);
			dataSource.setAcquireIncrement(acquire_increment);
			dataSource.setMaxStatements(max_statements);
			
			dataSource.setPreferredTestQuery(peferredTestQuery);
			dataSource.setMaxConnectionAge(maxConnectionAge);
			dataSource.setCheckoutTimeout(time_out);
			dataSource.setIdleConnectionTestPeriod(idle_test_period);
			dataSource.setAutoCommitOnClose(autoCommitOnClose);
			dataSource.setMaxIdleTime(max_idle_time);
			
			dataSource.setTestConnectionOnCheckin(validateOncheckIn);
			dataSource.setNumHelperThreads(numHelperThreads);
			
			dataSources.add(dataSource);
			return dataSource;
		} 
		catch (PropertyVetoException e) {
			dataSource.close();
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void destory(){
		for(ComboPooledDataSource dataSource:dataSources)
			dataSource.close();
	}

}
